<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/6/15
 * Time: 下午5:28
 */

namespace app\api\controller;


use app\common\model\CardUserGet;
use app\common\model\Deposit;
use app\common\model\DeviceMicro;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\PrintReceipt;
use app\common\model\Tklog;

class Prints extends Pub
{
    private $mid;


    public function getprintinfo($param)
    {
        file_put_contents('/data2/html/osapi/printinfo_get.txt', json_encode($param), 8);
       if($param['search_key']!==""){
        switch ($param['print_type']) {
            //支付
            case "pay":
                $re_param = $this->pay_tpl($param);
                break;
            //补打订单
            case "pay_bd":
                $re_param = $this->pay_bd_tpl($param);
                break;
            //卡券核销
            case "cardqcon":
                $re_param = $this->cardqcon_tpl($param);
                break;
            //积分充值
            case "cardbonusrec":
                $re_param = $this->cardbonusrec_tpl($param);
                break;
            //余额充值
            case "cardbalancerec":
                $re_param = $this->cardbalancerec_tpl($param);
                break;
            //积分消费
            case "cardbonuscon":
                $re_param = $this->cardbonuscon_tpl($param);
                break;
            //余额消费
            case "cardbalancecon":
                $re_param = $this->cardbalancecon_tpl($param);
                break;
            case "orderrefund":
                $re_param = $this->orderrefund_tpl($param);
                break;
            case "banjie":
                $re_param = $this->banjie_tpl($param);
                break;
            case "depositpay":
                $re_param = $this->depositpay_tpl($param);
                break;
            case "depositrefund":
                $re_param = $this->depositrefund_tpl($param);
                break;
        }

       }else{
           $re_param['return_code'] = "SUCCESS";
           $re_param['return_msg'] = '';
           $re_param['result_code'] = 'FAIL';
           $re_param['result_msg'] = '打印出错，如需小票烦请补打订单小票';
           $re_param['info'] = '打印出错，如需小票烦请补打订单小票';
       }
        return $re_param;


    }

    private function pay_tpl($param)
    {

        $map['orderid'] = $param['search_key'];
        $payinfo = $this->getorderinfo($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $title = str_replace("{title}", getpayfs($payinfo['channel']), $title);
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "会员卡折扣:" . $payinfo['dis_card_zk'] . "\r\n" . "          会员等级折扣：" . $payinfo['vip_card_zk'] . "\r\n" . "          卡券折扣:" . $payinfo['gel_card_zk'] . "\r\n" . "          卡券折扣详情:" . $payinfo['gel_info'];
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);
        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{ought}", $payinfo['ought_fee'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;


    }

    private function pay_bd_tpl($param)
    {
        $map['orderid'] = $param['search_key'];
        $payinfo = $this->getorderinfo($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $title = str_replace("{title}", getpayfs($payinfo['channel']), $title);
        $rarr['title'] = $title;
        $info = $printtpl['info'];
        $addo_popup = "会员卡折扣:" . $payinfo['dis_card_zk'] . "\r\n" . "        会员等级折扣：" . $payinfo['vip_card_zk'] . "\r\n" . "       卡券折扣:" . $payinfo['gel_card_zk'] . "\r\n" . "       卡券折扣详情:" . $payinfo['gel_info'];
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);
        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{ought}", $payinfo['ought_fee'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;


    }

    //卡券核销
    private function cardqcon_tpl($param)
    {

        $cardinfo = $this->get_card_con_info($param['search_key']);
        $printtpl = $this->gettpl($param);
//        return $cardinfo;
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{card_name}", $cardinfo['card_name'], $info);
        $info = str_replace("{card_code}", $cardinfo['card_code'], $info);
        $info = str_replace("{card_type}", $cardinfo['card_type'], $info);
        $info = str_replace("{paytime}", $cardinfo['end_time'], $info);
        $info = str_replace("{cashier}", $cardinfo['device_id'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        $info=str_replace("{total}",$payinfo['total_fee'],$info);
//        $info=str_replace("{ought}",$payinfo['ought_fee'],$info);
//        $info=str_replace("{addo_popup}",$addo_popup,$info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;


    }

    //积分充值
    private function cardbonusrec_tpl($param)
    {


        $payinfo = $this->get_card_bonus_rec_info($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{channel}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{card_name}", $payinfo['card_name'], $info);
        $info = str_replace("{card_num}", $payinfo['card_num'], $info);
        $info = str_replace("{num}", $payinfo['add_bonus'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;

    }

    //余额充值
    private function cardbalancerec_tpl($param)
    {

        $map['orderid'] = $param['search_key'];
        $payinfo = $this->get_card_balance_rec_info($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $title = str_replace("{title}", getpayfs($payinfo['channel']), $title);
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);
        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{ought}", $payinfo['ought_fee'], $info);
        $info = str_replace("{cardname}", $payinfo['card_name'], $info);
        $info = str_replace("{cardnum}", $payinfo['card_num'], $info);
        $info =str_replace("{recbalance}", $payinfo['add_balance'], $info);;      //充值金额
        $info = str_replace("{zsbalance}", $payinfo['zs_balance'], $info);            //赠送金额
        $info = str_replace("{zsbonus}", $payinfo['zs_bonus'], $info);              //赠送积分
        $info =str_replace("{zscardq}", $payinfo['zs_cardq'], $info);;      //赠送卡券
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;

    }

    //积分核销
    private function cardbonuscon_tpl($param)
    {


        $payinfo = $this->get_card_bonus_con_info($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{channel}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{card_name}", $payinfo['card_name'], $info);
        $info = str_replace("{card_num}", $payinfo['card_num'], $info);
        $info = str_replace("{num}", $payinfo['del_bonus'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
        $rarr['info'] = $info;
        return $rarr;

    }

    //余额核销
    private function cardbalancecon_tpl($param)
    {

        $map['orderid'] = $param['search_key'];
        $payinfo = $this->getorderinfo($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "会员卡折扣:" . $payinfo['dis_card_zk'] . "\r\n" . "         会员等级折扣：" . $payinfo['vip_card_zk'] . "\r\n" . "         卡券折扣:" . $payinfo['gel_card_zk'] . "\r\n" . "         卡券折扣详情:" . $payinfo['gel_info'];
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);
        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{ought}", $payinfo['ought_fee'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;

    }
    private function orderrefund_tpl($param)
    {

        $map['orderid'] = $param['search_key'];
        $payinfo = $this->get_order_fund($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);
        $info = str_replace("{refundno}", $payinfo['refundorderno'], $info);
//        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);       refund
        $info = str_replace("{refundtime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{ought}", $payinfo['ought_fee'], $info);
        $info = str_replace("{refund}", $payinfo['refund_fee'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;

    }
    private function depositpay_tpl($param){

        $map['orderid'] = $param['search_key'];
        $payinfo = $this->getorderinfo($param['search_key']);
        $printtpl = $this->gettpl($param);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $title = str_replace("{title}", getpayfs($payinfo['channel']), $title);
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{merchantname}", $payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);
        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $payinfo['device_id'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{ought}", $payinfo['ought_fee'], $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;

    }
    private function depositrefund_tpl($param){
        $map['orderid']=$param['search_key'];
        $Deposit=new Deposit();
        $depositinfo=$Deposit->where($map)->find();
        $payinfo = $this->getorderinfo($param['search_key']);
        $rarr['return_code'] = "SUCCESS";
        $rarr['return_msg'] = '';
        $rarr['result_code'] = 'SUCCESS';
        $rarr['result_msg'] = '';
        $printtpl = $this->gettpl($param);
        $rarr['x'] = $printtpl['x'];
        $rarr['y'] = $printtpl['y'];
        $rarr['bmpsize1'] = $printtpl['bmpsize1'];
        $rarr['bmpsize2'] = $printtpl['bmpsize2'];
        $title = $printtpl['title'];
        $title = str_replace("{title}", "酒店预授权", $title);
        $rarr['title'] = $title;

        $info = $printtpl['info'];
        $addo_popup = "";
        $info = str_replace("{merchantname}",$payinfo['merchantname'], $info);
        $info = str_replace("{no}", $payinfo['out_trade_no'], $info);

        $info = str_replace("{paymode}", getpayfs($payinfo['channel']), $info);
        $info = str_replace("{paytime}", $payinfo['end_time'], $info);
        $info = str_replace("{cashier}", $depositinfo['out_user'], $info);
        $info = str_replace("{refund_fee}", $depositinfo['out_fee'], $info);
        $info = str_replace("{total}", $payinfo['total_fee'], $info);
        $info = str_replace("{refund_time}", date('Y-m-d H:i:s',$depositinfo['out_time']), $info);
        $info = str_replace("{addo_popup}", $addo_popup, $info);
//        str_replace("{no}",$payinfo['out_trade_no'],$info);
        $rarr['info'] = $info;
        return $rarr;


    }
    private function banjie_tpl($param){
        $params=explode("|",$param['search_key']);
        $start_time = strtotime($params['0']);
        $end_time = strtotime($params['1']);
        $param['merchantid']=DeviceMicro::where(array('device_code' => $param['device_code']))->value('mid');
        if ($start_time == "" && $end_time == "") {
            $map['a.add_time'] = array(array('egt', 0), array('elt', time()), 'and');
        } elseif ($start_time !== "" && $end_time == "") {
            $map['a.add_time'] = array(array('egt', $start_time), array('elt', time()));
        } elseif ($start_time == "" && $end_time !== "") {
            $map['a.add_time'] = array(array('egt', 0), array('elt', $end_time));
        } else {
            $map['a.add_time'] = array(array('egt', $start_time), array('elt', $end_time));
        }
        $map['a.kuantai']=$param['kuantai'];
        $map['a.d_cashier']=$param['device_id'];
        $map['a.pay_statu']=array(array('neq', 0), array('neq', 8), 'and');
        $mids = $param['merchantid'];
        $map['a.mid'] = $mids;
        $maptk['addtime'] = array(array('egt', $start_time), array('elt', $end_time), "and");
        $maptk['mid'] = $mids;
        $maptk['action_user'] = $param['device_id'];
        $Tklog = new Tklog();
        $Paylog = new Paylog();
        $arrs = $Tklog->where($maptk)->field('sum(refund_fee) as refund_fee,count(id) as refund_order')->find();
        $arr['refund_order'] = $arrs['refund_order'];        //总退款数
        $arr['refund_fee'] = !empty($arrs['refund_fee']) ? $arrs['refund_fee'] : "0.00";          //总退款
        $map7 = $map;
        $map7['a.pay_fs'] = array('in', [0, 1, 2, 3, 4, 5, 8]);
        $z_arr = $Paylog->alias('a')->where($map7)->field('sum(price) as real_fee,sum(service_charge) as service_charge,count(id) as real_order,sum(yf_price) as ought_fee,sum(zk_price) as dis_fee,avg(price) as avg_fee')->find();
        $real_fee = (string)($z_arr['real_fee'] - $arr['refund_fee']);
        $arr['real_fee'] = number_format($real_fee, 2);
        $arr['order'] = $z_arr['real_order'];
        $arr['dis_fee'] = number_format($z_arr['dis_fee'], 2);          //总实收金额
        $arr['ought_fee'] = number_format($z_arr['ought_fee'], 2);          //总应付
        $arr['merchantname']=getmerchant($param['merchantid']);
        $printtpl = $this->gettpl($param);
        $info = $printtpl['info'];
        $map8 = $map;
        $map8['a.pay_fs'] = array('in', [0, 2]);
        $zwx_arr = $Paylog->alias('a')->where($map8)->field('sum(price) as real_fee,sum(service_charge) as service_charge,count(id) as real_order,sum(yf_price) as ought_fee,sum(zk_price) as dis_fee,avg(price) as avg_fee')->find();
        $map9 = $map;
        $map9['a.pay_fs'] = array('in', [1, 3]);
        $zali_arr = $Paylog->alias('a')->where($map9)->field('sum(price) as real_fee,sum(service_charge) as service_charge,count(id) as real_order,sum(yf_price) as ought_fee,sum(zk_price) as dis_fee,avg(price) as avg_fee')->find();
        $wxstatis="微信合计：".$zwx_arr['real_order']."单  ".$zwx_arr['ought_fee']."元";
        $alistatis="支付宝合计：".$zali_arr['real_order']."单  ".$zali_arr['ought_fee']."元";
        $paystatis="收款合计：".$arr['order']."单  ".$arr['ought_fee']."元"."\r\n"."    退款合计:".$arr['refund_order']."单  ".$arr['refund_fee']."元"."\r\n". "    折扣合计：".$arr['dis_fee']."元";

        $info = str_replace("{merchantname}", $arr['merchantname'], $info);
        $info = str_replace("{change_time1}",$params['0'] , $info);
        $info = str_replace("{change_time2}",$params['1'] , $info);
        $info = str_replace("{real}", $arr['real_fee']."元" , $info);
        $info = str_replace("{cashier}",$param['device_id']  , $info);
        $info = str_replace("{kuantai}",$param['kuantai']  , $info);
        $info = str_replace("{wxstatis}",$wxstatis  , $info);
        $info = str_replace("{alistatis}",$alistatis  , $info);
        $info = str_replace("{paystatis}",$paystatis, $info);
        $rarr['title']= $printtpl['title'];
        $rarr['info'] = $info;
        return $rarr;


    }
    private function gettpl($param)
    {

        $DeviceMicro = new DeviceMicro();
        $mid = $DeviceMicro->where(array('device_code' => $param['device_code']))->value('mid');
        $PrintReceipt = new PrintReceipt();
        $tpl = $PrintReceipt->where(array('mid' => $mid, 'cate' => $param['print_type'] . "_tpl"))->find();
        if ($tpl) {
            $printtpl = $tpl;

        } else {
            $printtpl = $PrintReceipt->where(array('type' => 'common', 'cate' => $param['print_type'] . "_tpl"))->find();
        }
        return $printtpl;

    }

}